﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;
using AgNed.Models;
using AgNed.DAO;
using AgNed.Controllers;


namespace AgNed.DAO
{
    public class DAOContactInfo : DAO
    {
        public bool addContactInfo(ContactInfo contactinfo)        //ADD METHOD
        {
            bool result = true;
            Connect();

            try
            {
                SqlCommand cmd = new SqlCommand(
                    "INSERT INTO ContactInfo (name, phone) " +
                    "VALUES (@name, @phone)", Connection);

                cmd.Parameters.Add(new SqlParameter("@name", contactinfo.name));
                cmd.Parameters.Add(new SqlParameter("@phone", contactinfo.phone));
           

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                result = false;
            }
            finally
            {
                Disconnect();
            }
            return result;
        }

        

        public List<ContactInfo> GetAllRecords()        //GETALL METHOD
        {
            Connect();
            List<ContactInfo> recordList = new List<ContactInfo>();

            try
            {
                SqlCommand command = new SqlCommand("SELECT * FROM ContactInfo", Connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    ContactInfo contact = new ContactInfo();
                    contact.id = Convert.ToInt32(reader["id"]);
                    contact.name = Convert.ToString(reader["name"]);
                    contact.phone = Convert.ToInt32(reader["phone"]);
                    recordList.Add(contact);
                }
                reader.Close();
            }

            catch (Exception)
            {
                // Не обрабатывать исключительные ситуации - дурной тон
            }

            finally
            {
                Disconnect();
            }
            return recordList;
        }
    }
}
